-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: useSSE() basic implement #60
Conversation
const fullURL = buildCompletedURL(baseURL, url, params); | ||
const eventSource = new EventSource(fullURL, { withCredentials: config.withCredentials }); | ||
|
||
const { data, update } = useRequest(handler, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么要通过useRequest来创建data,是不是直接$(config.initialData)
就可以了
const createSSEEvent = (eventName: string, event: MessageEvent<any> | Event) => { | ||
if (eventName === 'open') { | ||
return { | ||
method: methodInstance, | ||
eventSource | ||
} as AlovaSSEEvent; | ||
} | ||
if (eventName === 'error') { | ||
return { | ||
method: methodInstance, | ||
eventSource, | ||
error: new Error('sse error') | ||
} as AlovaSSEErrorEvent; | ||
} | ||
|
||
// 其余名称的事件都是(类)message 的事件,data 交给 dataHandler 处理 | ||
return { | ||
method: methodInstance, | ||
eventSource, | ||
data: dataHandler((event as MessageEvent).data) | ||
} as AlovaSSEMessageEvent<any>; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可能你没注意到createHookEvent
这个文件,应该是可以使用这个函数,然后调整下对SSE的事件对象的支持能用上。
|
||
return { | ||
readyState: _exp$(readyState), | ||
data, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个最好也可以_exp$一下,在react中可直接用
if (callbacks) { | ||
return forEach(callbacks, fn => fn(args)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个判断好像无效,因为按你写的callbacks至少都会是一个空数组
const setCallback = (fn: Fn) => { | ||
if (!callbacks.includes(fn)) { | ||
callbacks.push(fn); | ||
onCallbackChange && onCallbackChange(callbacks); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我提下我的见解,如果我的话,我会把onCallbackChange
接收处默认为noop
,这样就不需要多个地方判断onCallbackChange
是否有值,当然你这样写也没啥问题。
|
||
// type: eventname & useCallback() | ||
const eventMap: Map<string, ReturnType<typeof useCallback>> = new Map(); | ||
const [onOpen, triggerOnOpen, offOpen] = useCallback<SSEOnOpenTrigger>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得这个useCallback非常棒
No description provided.